<template>
  <view class="page-container">
    <addressSearchFixed
      ref="setAddres"
      @searchBtn="event_clickSearchBtn"
      :isShowAddress="false"
    ></addressSearchFixed>
    <view class="content" :style="[{ top: CustomBar + 46 + 'px' }]">
      <view class="view-box">
        <view class="view-item" @click="viewItemClick(0)">
          <image
            src="@/static/fabucheliang.png"
            style="height: 43rpx; width: 52rpx"
          />
          <text class="view-item-text">发布寻车</text>
        </view>
        <view class="view-item" @click="viewItemClick(1)">
          <image src="@/static/car.png" style="height: 41rpx; width: 47rpx" />
          <text class="view-item-text">我的寻车</text>
        </view>
        <view class="view-item" @click="viewItemClick(2)">
          <image
            src="@/static/fanlibaojia.png"
            style="height: 45rpx; width: 50rpx"
          />
          <text class="view-item-text">我的报价</text>
        </view>
      </view>
      <view class="search-type">
        <view class="type-item" @click="modelClick">
          <text>{{ currentModel.name || "全部车型" }}</text>
          <image src="@/static/xiala.png"></image>
        </view>
        <view
          class="type-item"
          @click="typeClick"
          :class="{ active: typeSelected }"
        >
          <text>{{ typesName }}</text>
          <image src="@/static/xiala.png"></image>
        </view>
        <view
          class="type-item"
          @click="areaClick"
          :class="{ active: locationSelected }"
        >
          <text>{{ locationName }}</text>
          <image src="@/static/xiala.png"></image>
        </view>
      </view>
      <scroll-view
        class="scroll-view"
        scroll-y="true"
        :scroll-top="scrollTop"
        refresher-enabled
        :refresher-triggered="triggered"
        @refresherrefresh="onRefresh"
        @scrolltolower="lower"
      >
        <view class="car-list">
          <view
            class="car-item"
            v-for="(item, index) in carList"
            :key="index"
            @click="carClick(item)"
          >
            <image
              v-if="item.topping === 1"
              class="top"
              src="@/static/topimg.png"
              mode=""
            ></image>
            <view class="car-name">
              <text class="name">{{ item.cars_name }}</text>
              <text class="tag" v-if="item.recommend == 1">推荐</text>
            </view>
            <view class="tag-list">
              <view
                class="tag-item"
                v-for="(label, labelIndex) in item.label"
                :key="labelIndex"
                >{{ label }}</view
              >
            </view>
            <view class="tips" v-if="item.describe">{{ item.describe }}</view>
            <view class="bj-date">
              <view class="bj">已有{{ item.quotation_number }}家车商报价</view>
              <view class="date">{{ item.time }}</view>
            </view>
            <view class="item-bottom">
              <view class="user">
                <image :src="item.imageurl" mode="" class="user-img"></image>
                <text class="user-name">{{ item.release }}</text>
                <image :src="item.users_icon" mode="" class="user-type"></image>
              </view>
              <view class="bjz">
                <image
                  v-if="item.member_quotations == 1"
                  src="@/static/memberbjz.png"
                  mode=""
                ></image>
              </view>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
    <!-- 类型 -->
    <uni-popup
      ref="bottom_popup_types"
      :animation="true"
      type="bottom"
      @maskClick="typeSelected = false"
    >
      <view class="types-view-box-class">
        <view
          v-for="(item, index) in sourceTypeList"
          :key="index"
          @click="types_click(item, index)"
        >
          <view class="item-view-box-search-type">
            <view
              :class="
                index == types_select
                  ? 'types-name-class-select'
                  : 'types-name-class'
              "
              >{{ item.name }}
            </view>
            <view
              :class="
                index == types_select
                  ? 'types-name-class-select'
                  : 'types-number-class'
              "
              >{{ item.num }}
            </view>
          </view>
        </view>
      </view>
    </uni-popup>
    <!-- 地区 -->
    <uni-popup
      ref="bottom_popup_province"
      :animation="true"
      type="bottom"
      @maskClick="locationSelected = false"
    >
      <view class="types-view-box-class">
        <view
          v-for="(item, index) in provinceList"
          :key="index"
          @click="location_click(item, index)"
        >
          <view class="item-view-box-search-type">
            <view
              :class="
                index == location_select
                  ? 'types-name-class-select'
                  : 'types-name-class'
              "
              >{{ item.name }}
            </view>
            <view
              :class="
                index == location_select
                  ? 'types-name-class-select'
                  : 'types-number-class'
              "
              >{{ item.num }}
            </view>
          </view>
        </view>
      </view>
    </uni-popup>
    <allBrand />
  </view>
</template>

<script>
import addressSearchFixed from "@/components/addressSearch/addressSearchFixed.vue";
import {
  newsCarsSearchProvince,
  newsCarsSearchReleaseList,
  newsCarsSearchCarSourceType,
} from "../../../api/newCarSerach.js";
export default {
  components: {
    addressSearchFixed,
  },
  data() {
    return {
      CustomBar: this.CustomBar,
      sreach: "",
      carList: [],
      scrollTop: 0,
      page: 1, //第几页数据
      page_num: 5,
      last_page: 0, //总页数
      isShowMore: false,
      isEnd: false,
      triggered: false,
      isfreshing: false,
      isShowList: false,
      isNoData: false,
      sourceTypeList: [],
      typesName: "全部类型",
      types_select: "0",
      typeSelected: false,
      provinceList: [],
      locationName: "全部地区",
      locationSelected: false,
      location_select: "0",
      currentModel: {},
    };
  },
  mounted() {
    this.getsearchCarListData();
	newsCarsSearchCarSourceType().then((res) => {
	  this.sourceTypeList = res.data;
	});
	newsCarsSearchProvince().then((res) => {
	  this.provinceList = res.data;
	});
  },
  methods: {
    event_clickSearchBtn(content) {
      this.sreach = content;
      this.getsearchCarListData();
    },
    viewItemClick(type) {
      if (type === 0) {
        uni.navigateTo({
          url: "/pages/home/newCarSearch/releaseCar/releaseCar",
        });
      } else if (type === 1) {
        uni.navigateTo({
          url: "/pages/home/newCarSearch/myCarSearch/myCarSearch",
        });
      } else if (type === 2) {
        uni.navigateTo({
          url: "/pages/home/newCarSearch/myQuotes/myQuotes",
        });
      }
    },
    // 跳转发布车源
    releaseCar() {
      uni.navigateTo({
        url: "/pages/home/newCarSearch/releaseCar/releaseCar",
      });
    },
    // 点击车辆跳转到寻车报价
    carClick(item) {
      uni.navigateTo({
        url: `/pages/home/newCarSearch/quotation/quotation?id=${item.id}`,
      });
    },
    //获取寻车列表
    getsearchCarListData() {
      var that = this;
      newsCarsSearchReleaseList(
        this.sreach,
        this.source_type_id,
        this.registration_province,
        this.series_id,
        this.page,
        this.page_num
      ).then((res) => {
        that.$nextTick(() => {
          if (that.page == 1) {
            that.carList = [];
            that.carList = res.data.data;
            that.triggered = false;
            that.isfreshing = false;
          } else {
            for (var i = 0; i < res.data.data.length; i++) {
              that.carList.push(res.data.data[i]); // 加载的数据追加在上一页后面
            }
            that.isEnd = false;
          }
          that.last_page = res.data.last_page;

          if (that.last_page == that.page) {
            that.isShowMore = true;
            setTimeout(() => {
              that.isShowMore = false;
            }, 2000);
          } else {
            that.isShowMore = false;
          }
          if (res.data.total > 0) {
            that.isNoData = false;
            that.isShowList = true;
          } else {
            that.isShowList = false;
            that.isNoData = true;
          }
          setTimeout(() => {
            that.$forceUpdate();
          }, 700);
        });
      });
    },
    onRefresh() {
      if (!this.triggered) {
        if (this.isfreshing) return;
        this.isfreshing = true;
        if (!this.triggered) {
          this.triggered = true;
        }
        this.page = 1;
        this.getsearchCarListData();
      }
    },

    lower() {
      if (!this.isShowMore) {
        if (!this.isEnd) {
          if (this.page < this.last_page) {
            this.page = this.page + 1;
            this.getsearchCarListData();
          } else {
            uni.showToast({
              title: "没有数据了",
              icon: "none",
            });
          }
        }
      }
      if (this.isShowMore) {
        var that = this;
        that.isShowMore = true;
        setTimeout(() => {
          that.$nextTick(() => {
            that.isShowMore = false;
          });
        }, 2500);
      }
    },
    typeClick() {
	  this.typeSelected = true;
	  this.$refs.bottom_popup_types.open();
    },
    // 选择车型
    modelClick() {
      uni.navigateTo({
        url: "/pages/home/newCarSearch/searchModel/searchModel",
      });
    },
    backData(data) {
      this.currentModel = data;
      this.series_id = data.id;
      this.getsearchCarListData();
    },
    //选择类型
    types_click(item, index) {
      this.types_select = index;
      this.source_type_id = item.id;
      this.typesName = item.name;
      this.getsearchCarListData();
      this.typeSelected = false;
      this.$refs.bottom_popup_types.close();
    },
    areaClick() {
	   this.$refs.bottom_popup_province.open();
    },
    //选择地区
    location_click(item, index) {
      this.location_select = index;
      this.registration_province = item.id;
      this.locationName = item.name;
      this.getsearchCarListData();
      this.locationSelected = false;
      this.$refs.bottom_popup_province.close();
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  color: #000;
}
.page-container {
  background: #f7f8fa;
}
::v-deep .header-class {
  background-color: #2d56c5 !important;
}
.content {
  position: fixed;
  left: 0;
  right: 0;
  top: 140px;
}
.scroll-view {
  height: 100vh;
}
.view-box {
  background-color: #2d56c5;
  display: flex;
  gap: 25rpx;
  padding: 30rpx 28rpx;
  .view-item {
    padding: 15rpx 0;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #5778d1;
    border-radius: 20rpx;
    color: #fff;
    text {
      margin-top: 15rpx;
    }
  }
}
.search-type {
  background-color: #fff;
  height: 85rpx;
  padding: 0 28rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  .type-item {
    display: flex;
    align-items: center;
    image {
      width: 16rpx;
      height: 10rpx;
    }
    text {
      margin-right: 12rpx;
      font-size: 28rpx;
      font-family: PingFang SC;
      font-weight: 500;
    }
    &.active {
      text {
        color: #0e59ea;
      }
    }
  }
  .pop-box {
    width: 100%;
    height: 500rpx;
    background-color: #fff;
    position: absolute;
    left: 0;
    right: 0;
    top: 85rpx;
    z-index: 99;
  }
}
.car-list {
  padding: 0 28rpx;
  background: #f7f8fa;
  padding-bottom: 580rpx;
  .car-item {
    background-color: #fff;
    padding: 30rpx 20rpx;
    border-radius: 14rpx;
    margin-top: 14rpx;
    position: relative;
    .top {
      width: 96rpx;
      height: 96rpx;
      position: absolute;
      top: 0;
      right: 0;
    }
    .car-name {
      display: flex;
      padding-right: 100rpx;
      .name {
        font-size: 32rpx;
        font-weight: 800;
      }
      .tag {
        margin-top: 10rpx;
        width: 60rpx;
        min-width: 60rpx;
        height: 32rpx;
        line-height: 32rpx;
        margin-left: 30rpx;
        text-align: center;
        background: linear-gradient(-90deg, #e51b2e 0%, #f56346 100%);
        border-radius: 4rpx;
        font-size: 20rpx;
        color: #fff;
        white-space: nowrap;
      }
    }

    .tag-list {
      margin-top: 20rpx;

      .tag-item {
        margin-right: 12rpx;
        height: 48rpx;
        line-height: 48rpx;
        padding: 0 25rpx;
        font-size: 28rpx;
        font-weight: 800;
        color: #2d56c5;
        background: #edf1ff;
        border-radius: 4rpx;
        display: inline-block;
      }
    }

    .tips {
      margin-top: 20rpx;
      font-size: 22rpx;
      font-weight: 500;
    }

    .bj-date {
      margin-top: 20rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .bj {
        font-size: 24rpx;
        font-family: PingFang SC;
        font-weight: bold;
        color: #ec4b4b;
      }

      .date {
        font-size: 24rpx;
        font-family: PingFang SC;
        font-weight: 500;
        color: #999999;
      }
    }

    .item-bottom {
      margin-top: 18rpx;
      padding-top: 18rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-top: 1rpx solid #f2f2f2;
      .user {
        display: flex;
        align-items: center;
        image.user-img {
          width: 50rpx;
          height: 50rpx;
        }

        text.user-name {
          margin: 0 10rpx 0 20rpx;
          font-size: 20rpx;
          font-family: PingFang SC;
          font-weight: 500;
          color: #010101;
          white-space: nowrap;
        }

        image.user-type {
          margin-top: 5rpx;
          width: 22rpx;
          height: 26rpx;
        }
      }
      .bjz {
        image {
          width: 172rpx;
          height: 32rpx;
        }
      }
    }
  }
}
.release-car {
  width: 100%;
  height: 162rpx;
  background: #ffffff;
  box-shadow: 0rpx -6rpx 4rpx 0rpx rgba(204, 204, 204, 0.2);
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 0 28rpx;
  display: flex;
  align-items: center;
  view {
    flex: 1;
    height: 80rpx;
    line-height: 80rpx;
    background: #0e59ea;
    border-radius: 14rpx;
    font-size: 28rpx;
    font-family: PingFang SC;
    font-weight: 500;
    color: #ffffff;
    text-align: center;
  }
}
.types-view-box-class {
  height: auto;
  background-color: #ffffff;
  opacity: 1;
  border-radius: 28rpx 28rpx 0rpx 0rpx;
  padding: 30rpx 28rpx 56rpx 36rpx;
}

.all-types-class {
  text-align: center;
  font-size: 32rpx;
  font-family: Alimama ShuHeiTi;
  font-weight: bold;
  color: #1c2125;
  opacity: 1;
  margin-bottom: 14rpx;
}

.item-view-box-search-type {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 14rpx 0rpx;
}

.types-name-class {
  font-size: 28rpx;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #1c2125;
  opacity: 1;
}

.types-name-class-select {
  font-size: 28rpx;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #0e59ea;
  opacity: 1;
}

.types-number-class {
  font-size: 28rpx;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #999999;
  opacity: 1;
}
</style>